package com.ruoyi.cat.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.cat.domain.*;
import com.ruoyi.cat.mapper.AdTicketRollMapper;
import com.ruoyi.cat.service.IAdMediumService;
import com.ruoyi.cat.service.IAdTicketImageService;
import com.ruoyi.cat.service.IAdTicketLikeService;
import com.ruoyi.cat.service.IAdTicketRollService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 票卷相关内容Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-16
 */
@Service
public class AdTicketRollServiceImpl implements IAdTicketRollService
{
    @Autowired
    private AdTicketRollMapper adTicketRollMapper;

    @Autowired
    private IAdTicketImageService adTicketImageService;

    @Autowired
    private IAdTicketLikeService adTicketLikeService;

    /**
     * 查询票卷相关内容
     * 
     * @param id 票卷相关内容主键
     * @return 票卷相关内容
     */
    @Override
    public AdTicketRoll selectAdTicketRollById(Long id)
    {
        AdTicketRoll adTicketRoll =   adTicketRollMapper.selectAdTicketRollById(id);
        AdTicketImage adTicketImage = new AdTicketImage();
        adTicketImage.setTicketId(adTicketRoll.getId());
        List<AdTicketImage> adTicketImageList = adTicketImageService.selectAdTicketImageList(adTicketImage);
        adTicketRoll.setTicketImageList(adTicketImageList);
        return adTicketRoll;
    }

    @Override
    public AdTicketRoll selectAdTicketRollByIdApp(Long id, Long userId) {
        AdTicketRoll adTicketRoll =   adTicketRollMapper.selectAdTicketRollById(id);
        AdTicketImage adTicketImage = new AdTicketImage();
        adTicketImage.setTicketId(adTicketRoll.getId());
        List<AdTicketImage> adTicketImageList = adTicketImageService.selectAdTicketImageList(adTicketImage);
        if (userId != null) {
            AdTicketLike adGiftLike = new AdTicketLike();
            adGiftLike.setUserId(userId);
            adGiftLike.setGiftId(id);
            List<AdTicketLike> list = adTicketLikeService.selectAdTicketLikeList(adGiftLike);
            adTicketRoll.setLikeSatate(list.size() > 0 ? "1" : "0");
        }
        adTicketRoll.setTicketImageList(adTicketImageList);
        return adTicketRoll;
    }


    @Override
    public AdTicketRoll selectAdTicketRollByIdBy(Long id) {
        return adTicketRollMapper.selectAdTicketRollById(id);
    }

    /**
     * 查询票卷相关内容列表
     * 
     * @param adTicketRoll 票卷相关内容
     * @return 票卷相关内容
     */
    @Override
    public List<AdTicketRoll> selectAdTicketRollList(AdTicketRoll adTicketRoll)
    {
        List<AdTicketRoll> list = adTicketRollMapper.selectAdTicketRollList(adTicketRoll);
        list.forEach(iAdTicketRoll->{
            AdTicketImage adTicketImage = new AdTicketImage();
            adTicketImage.setTicketId(iAdTicketRoll.getId());
            List<AdTicketImage> adTicketImageList = adTicketImageService.selectAdTicketImageList(adTicketImage);
            iAdTicketRoll.setTicketImageList(adTicketImageList);
        });
        return list;
    }

    @Override
    public List<AdTicketRoll> selectTicketListLimit(AdTicketRoll adTicketRoll) {
        List<AdTicketRoll> list =   adTicketRollMapper.selectTicketListLimit(adTicketRoll);
        list.forEach(iAdTicketRoll->{
            AdTicketImage adTicketImage = new AdTicketImage();
            adTicketImage.setTicketId(iAdTicketRoll.getId());
            List<AdTicketImage> adTicketImageList = adTicketImageService.selectAdTicketImageList(adTicketImage);
            iAdTicketRoll.setTicketImageList(adTicketImageList);
        });
        return list;
    }

    /**
     * 新增票卷相关内容
     * 
     * @param adTicketRoll 票卷相关内容
     * @return 结果
     */
    @Override
    public int insertAdTicketRoll(AdTicketRoll adTicketRoll)
    {
        adTicketRoll.setCreateTime(DateUtils.getNowDate());
        int i = adTicketRollMapper.insertAdTicketRoll(adTicketRoll);
        adTicketRoll.getTicketImageList().forEach(ticketImage->{
            ticketImage.setTicketId(adTicketRoll.getId());
            adTicketImageService.insertAdTicketImage(ticketImage);
        });
        return i;
    }

    /**
     * 修改票卷相关内容
     * 
     * @param adTicketRoll 票卷相关内容
     * @return 结果
     */
    @Override
    public int updateAdTicketRoll(AdTicketRoll adTicketRoll)
    {
       int i = adTicketRollMapper.updateAdTicketRoll(adTicketRoll);
        List<AdTicketImage> adGiftImageList = adTicketRoll.getTicketImageList();
        if (adGiftImageList != null) {
            adTicketImageService.deleteAdTicketImageById(adTicketRoll.getId());
            adTicketRoll.getTicketImageList().forEach(iAdGiftImage->{
                iAdGiftImage.setTicketId(adTicketRoll.getId());
                iAdGiftImage.setCreateTime(DateUtils.getNowDate());
                adTicketImageService.insertAdTicketImage(iAdGiftImage);
            });
        }
        return i;
    }

    @Override
    public int updateAdTicketRollBy(AdTicketRoll adTicketRoll) {
        return adTicketRollMapper.updateAdTicketRoll(adTicketRoll);
    }

    /**
     * 批量删除票卷相关内容
     * 
     * @param ids 需要删除的票卷相关内容主键
     * @return 结果
     */
    @Override
    public int deleteAdTicketRollByIds(Long[] ids)
    {
        return adTicketRollMapper.deleteAdTicketRollByIds(ids);
    }

    /**
     * 删除票卷相关内容信息
     * 
     * @param id 票卷相关内容主键
     * @return 结果
     */
    @Override
    public int deleteAdTicketRollById(Long id)
    {
        return adTicketRollMapper.deleteAdTicketRollById(id);
    }



}
